package edu.tcc.iot.persistence;

import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.sql.DataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class DaoConnectionFactory {

	public static DataSource createDataSource() {
		DbConnectionInfo info =  createDbConnectionInfo();
		MysqlDataSource dataSource = new MysqlDataSource();
		String url = info.getDriver() + "://" + info.getIp() + ":" + info.getPort() + "/" +  info.getSchema();
		dataSource.setUrl(url);
		dataSource.setUser(info.getUser());
		dataSource.setPassword(info.getPassword());
		
		try {
			dataSource.getConnection().setAutoCommit(true);
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return dataSource;
	}

	private static DbConnectionInfo createDbConnectionInfo() {
		ResourceBundle config = ResourceBundle.getBundle("db_configuration");
		return new DbConnectionInfo(
				config.getString("dao.driver"), 
				config.getString("dao.ip"), 
				config.getString("dao.port"), 
				config.getString("dao.schema"), 
				config.getString("dao.user"), 
				config.getString("dao.password"));
	}
}
